package com.macro.mall.tiny.modules.bms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.modules.bms.mapper.ConfigMapper;
import com.macro.mall.tiny.modules.bms.model.Config;
import com.macro.mall.tiny.modules.bms.service.ConfigService;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 公共配置表 服务实现类
 * </p>
 *
 * @author macro
 * @since 2025-11-19
 */
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService {

    @Autowired
    private ConfigMapper configMapper;

    @Override
    public Config getByKey(String key) {
        return configMapper.selectByKey(key);
    }

    @Override
    public CommonPage<Config> getPageList(String key, Integer pageNum, Integer pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<Config> configList = configMapper.selectPageList(key, offset, pageSize);
        int totalCount = configMapper.selectTotalCount(key);
        int totalPage = (int) Math.ceil((double) totalCount / pageSize);
        
        Page<Config> page = new Page<>(pageNum, pageSize);
        page.setRecords(configList);
        page.setTotal(totalCount);
        page.setPages(totalPage);
        
        return CommonPage.restPage(page);
    }

    @Override
    public boolean create(Config config) {
        config.setCreatedAt((int) (System.currentTimeMillis() / 1000));
        config.setUpdatedAt((int) (System.currentTimeMillis() / 1000));
        return this.save(config);
    }

    @Override
    public boolean update(Long id, Config config) {
        config.setId(id.intValue());
        config.setUpdatedAt((int) (System.currentTimeMillis() / 1000));
        return this.updateById(config);
    }

    @Override
    public boolean delete(Long id) {
        Config config = new Config();
        config.setId(id.intValue());
        config.setDelFlag(1);
        return this.updateById(config);
    }
}